package com.hope.androidproject.login;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hope.androidproject.MainActivity;
import com.hope.androidproject.R;
import com.hope.androidproject.listener.HttpCallBackListener;
import com.hope.androidproject.utils.FinalValue;
import com.hope.androidproject.utils.JsonUtil;
import com.hope.androidproject.utils.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends Activity implements View.OnClickListener {

    private EditText mUserEdit, mPwdEdit;
    private Button mLoginBtn;
    private TextView mForgetText, mRegisterText, mLookAroudText;
    private ProgressDialog mProgressDialog;

    private static final int SHOW_ERROR_INFO = 0x02;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_ERROR_INFO:
                    String info = (String) msg.obj;
                    Toast.makeText(LoginActivity.this, info, Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }
    };


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
    }

    private void initViews() {
        mUserEdit = (EditText) findViewById(R.id.username_edit);
        mPwdEdit = (EditText) findViewById(R.id.password_edit);
        mLoginBtn = (Button) findViewById(R.id.login_btn);
        mForgetText = (TextView) findViewById(R.id.forget_pwd);
        mRegisterText = (TextView) findViewById(R.id.register);
        mLookAroudText = (TextView) findViewById(R.id.look_around);
        //设置点击事件
        mLoginBtn.setOnClickListener(this);
        mForgetText.setOnClickListener(this);
        mRegisterText.setOnClickListener(this);
        mLookAroudText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.login_btn:
                //请求服务端数据
                String path = FinalValue.SERVER_IP + FinalValue.PROJECT_INFO + FinalValue.LOGIN;
                //拼接数据
                String userName = mUserEdit.getText().toString();
                String password = mPwdEdit.getText().toString();
                String postData = "userName=" + userName + "&" + "password=" + password;
                showDialog();
                NetUtil.sendRequest(path, "POST", postData, new HttpCallBackListener() {
                    @Override
                    public void onFinish(String response) {
                        Log.e("回传信息", response);
                        //将resource转换成Json数据
                        JSONObject jsonObject = JsonUtil.parseStringToJson(response);
                        try {
                            String jsonStr = jsonObject.getString("result");
                            if (jsonStr.equals("ok")) {
                                //跳转页面
                                intent.setClass(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                SharedPreferences.Editor edit = getSharedPreferences("data",MODE_PRIVATE).edit();
                                edit.putBoolean("isLogin",true);
                                edit.apply();
                                finish();
                            } else {
                                Message message = handler.obtainMessage();
                                message.what = SHOW_ERROR_INFO;
                                message.obj = "登陆失败，用户名或密码错误";
                                handler.sendMessage(message);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Message message = handler.obtainMessage();
                            message.what = SHOW_ERROR_INFO;
                            message.obj = "登陆失败，请检查你的网络连接";
                            handler.sendMessage(message);
                        }finally {
                            dismissDialog();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        Log.e("错误信息", "登陆失败");
                        e.printStackTrace();
                        dismissDialog();
                    }
                });
                break;
            case R.id.forget_pwd:
                //TODO 跳转到忘记密码页面
                break;
            case R.id.register:
                intent.setClass(this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.look_around:
                intent.setClass(this,MainActivity.class);
                startActivity(intent);
                break;
        }
    }

    //显示弹框
    public void showDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("正在登陆");
        }
        mProgressDialog.show();
    }

    //关闭
    public void dismissDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }
}
